﻿/// <reference Path="jquery-1.3.2-vsdoc.js" />
$(function() {
    window.ReLoadMiNiCar = function() {
        // 设置购物车显示层的大小
        var cart = $("#Cart"), c_p = cart.offset();
        $("#MyCartList").width(cart.width() + 30)
            .css({ top: (c_p.top + cart.height() + 10) + "px", left: c_p.left + "px" });
        $.get(miniCarUrl, { rnd: Math.random() }, function(result) {
            $("#Cart .carNumber").text($(result).find(".carNumber").text());
            $("#MyCartList").html(result);
            $MsShow("CartSwitch_wrap", "MyCartList");
        })
    };
    window.ReLoadMiNiCar();

    // 搜索自动完成
    var search = {
        d: null, c: 0, v: "",
        k: "#hidSearchKey", k_tmp: "#tbSearchKey_temp",
        css_sa: "SearchAuto", css_o: "SearchOver",
        keyCodes: { esc: 27, enter: 13, left: 37, up: 38, right: 39, down: 40 },
        show: function() { if (this.d) { $(this.d).show(); } },
        hide: function() { if (this.d) { $(this.d).hide(); }; this.c = 0; this.v = ""; },
        docClick: function(event) {
            if (event.originalEvent) {
                var elm = event.originalEvent.srcElement || event.originalEvent.target;
                if (("#" + $(elm).attr("id")) !== this.k_tmp) { this.hide(); }
            }
        },
        adEvent: function() {
            if (this.d) {
                var s = this, trs = $(s.d).find("tr");
                trs.hover(function() { $(this).addClass(s.css_o); },
                          function() { $(this).removeClass(); });
                trs.click(function() {
                    var v = $(this).find("td").eq(0).text();
                    $(s.k).val(v); $(s.k_tmp).val("");
                    window.location.href = psk_search_url.replace(/-key-/ig, encodeURI(v));
                });
            }
        },
        fireEvent: function(kc) {
            var s = this, ks = s.keyCodes, trs = $(s.d).find("tr"), max = trs.length - 1;
            if (kc == ks.up || kc == ks.down) {
                if (kc == ks.up) { s.c = s.c == 0 ? max : s.c - 1; }
                if (kc == ks.down) { s.c = s.c == max ? 0 : s.c + 1; }
                trs.mouseout(); trs.eq(s.c).mouseover();
                var v = trs.eq(s.c).find("td").eq(0).text();
                $(s.k_tmp).val(v); s.v = v;
            } else { if (kc == ks.enter) { trs.eq(s.c).click(); } }
        },
        adElement: function(o) {
            if (this.d) { return; }
            var s = $(o), w = s.width(), h = s.height(), pos = s.offset();
            var W = w + 4, L = pos.left, T = pos.top + h + 5;
            this.d = $('<div class="' + this.css_sa + '"></div').appendTo($(document.body));
            this.d.width(W).css({ top: T + "px", left: L + "px" });
        },
        adHtml: function(l) {
            var html = "";
            $(l).each(function(i, n) {
                html += "<tr><td>" + n.Keys + '</td>';
                html += '<td class="textRight">约 ' + n.Totals + " 条</td></tr>";
            });
            this.d.html("<table>" + html + "</table>");
        }
    };
    // 单击图片
    $(".S_input2").click(function() {
        window.location.href = psk_search_url.replace(/-key-/ig, encodeURI($(search.k_tmp).val()));
    });
    $(document).click(function(event) { search.docClick(event); });
    $(search.k_tmp).click(function() { $(this).select() }).keyup(function(event) {
        var s = $(this), kc = event.originalEvent.keyCode, v = s.val(), ks = search.keyCodes;
        search.adElement(this); if (kc == ks.esc) { s.val(""); search.hide(); }
        if (v == search.v) { } else {
            if (v == "") { search.hide(); } else {
                if (!(kc == ks.up || kc == ks.down || kc == ks.left || kc == ks.right)) {
                    search.v = v;
                    $.getJSON(psk_ajax_url.replace(/-id-/ig, encodeURI(v)), null, function(l) {
                        if (l === -1) { search.hide(); } else {
                            search.adHtml(l); search.adEvent(); search.show();
                        }
                    });
                }
            }
        }
        search.fireEvent(kc);
    });
});

